﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using AccessControl.Sida.Business;
using AccessControl.Sida.Common;
using AccessControl.Sida.StudentCardPrint.ReportBuilder;
using DevExpress.XtraEditors;


namespace AccessControl.Sida.StudentCardPrint.Form
{
	public partial class MainForm : XtraForm
	{
		private Utility.Grid.CheckMarkSelection _checkMarkSelection;


		readonly Business.StudentBasicInfoBusiness _studentBasicInfoBusiness = new Business.StudentBasicInfoBusiness();

		public MainForm()
		{
			InitializeComponent();

			
		}

		private void simpleButtonSearch_Click(object sender, EventArgs e)
		{
			GetData();
            //if (gridView.RowCount > 0)
            //    gridView.Focus();
            //else
            //    txtSearch.Focus();
		}
        private void Lookups_ApplyFilter(object sender, EventArgs e)
        {
            GetData();
        }

		private void GetData(){
		    try{
                var filters = new List<string>();
                if (!string.IsNullOrEmpty(txtSearch.Text)) filters.Add(string.Format("(fsf.stcode LIKE '{0}%' OR fsf.name LIKE N'%{0}%' OR fsf.family LIKE N'%{0}%' OR fsf.idd_meli LIKE '%{0}%' )", txtSearch.Text.Replace("'", "")));
                if (cmbTerm.FiltersCount > 0) filters.Add(cmbTerm.getFilterString().Replace("Vorood", "CAST((Sal_Vorod*10+nimsal_vorod) as varchar(3))"));
                if (cmbBranch.FiltersCount > 0) filters.Add(cmbBranch.getFilterString());
                var studentBasicInfoDataTable = _studentBasicInfoBusiness.SelectStudentByFilter(filters);
                studentBasicInfoDataTable.AcceptChanges();
                gridControl.DataSource = studentBasicInfoDataTable.DefaultView;
            }
		    catch (Exception ex){

		        XtraMessageBox.Show(this, ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
		    }

		}

		private String[] GetSelectedStudentCode()
		{
			var array = new string[_checkMarkSelection.SelectedCount];
			for (int i = _checkMarkSelection.selection.Count - 1; i >= 0; i--)
			{
				array[i] = String.Format("'{0}'", ((DataRowView)_checkMarkSelection.GetSelectedRow(i))["STCode"]);
			}
			return array;
		}

		private void MainForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
				if ( _checkMarkSelection.SelectedCount>0)
					simpleButtonPrintCard.PerformClick();
				else
					simpleButtonSubmit.PerformClick();
		}

		private void gridView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
		{
			if (e.Clicks != 2)
				return;
			simpleButtonPrintCard.PerformClick();
		}


		private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			Application.Exit();
		}

		private void simpleButtonPrintCard_Click(object sender, EventArgs e)
		{
			if (_checkMarkSelection.SelectedCount > 0)
				Utility.Report.ReportViewer.ViewTextReport(new TextReportBuilderStudentCard(GetSelectedStudentCode()), this, "کارت ورود به جلسه",
										false);
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			_checkMarkSelection = new Utility.Grid.CheckMarkSelection(gridView);
			_checkMarkSelection.SelectionChanged +=
				GridSelectionChanged;

            cmbTerm.BindData(SidaInfoBusiness.SelectTerms, "Vorood", "Vorood", "TermPersianCaption", null, null);
            cmbBranch.BindData(SidaInfoBusiness.SelectBranches, "id", "id", "nameresh", "namegroup", null);
        }

		private void GridSelectionChanged(object sender, Utility.Grid.CheckMarkSelection.SelectionChangedEventArgs e)
		{
			simpleButtonPrintCard.Enabled = _checkMarkSelection.SelectedCount > 0;
		}

	}
}